#include <iostream>

using namespace std;
const int N=3*1e4+10;

int t;
int f[N];
int d[N],s[N];
int find(int x)
{
	if(f[x]!=x)
	{
		int root=find(f[x]);
		d[x]+=d[f[x]];
		f[x]=root;
	}
	return f[x];
}
int main()
{
	for(int i=1;i<N;i++)
		f[i]=i,s[i]=1;
	cin>>t;
	while(t--)
	{
		char op[2];
		int x,y;
		scanf("%s%d%d",op,&x,&y);
		if(*op=='M')
		{
			int px=find(x),py=find(y);
			if(px!=py)
			{
				d[px]=s[py];
				s[py]+=s[px];
				f[px]=py;
			}
		}
		else{
			int px=find(x),py=find(y);
			if(px!=py)
			{
				cout<<-1<<endl;
			}
			else{
				cout<<max(0,abs(d[x]-d[y])-1)<<endl;
			}
		}
	}
	return 0;
}
